

package com.haoxi.gateway.smgpmsg;


import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;



public class SMGPQueryMessage extends SMGPMessage
{

    private StringBuffer strBuf;

    public SMGPQueryMessage(Date time, int queryType, String queryCode)
        throws IllegalArgumentException
    {
        if(time == null)
            throw new IllegalArgumentException(
            "The parameter of Query message is wrong:Time value is null.");
            
        if(queryType < 0 || queryType > 255)
            throw new IllegalArgumentException(
            "The parameter of Query message is wrong:QueryType less than 0 or larger than 255.");
            
        if(queryCode != null && queryCode.length() > 10)
            throw new IllegalArgumentException(
            "The parameter of Query message is wrong:QueryCode length is larger than 10");
            
        int len = 31;
        buf = new byte[len];
        TypeConvert.int2byte(len, buf, 0);
        TypeConvert.int2byte(SMGPCommon.SMGP_QUERY, buf, 4);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        System.arraycopy(dateFormat.format(time).getBytes(), 0, buf, 12, dateFormat.format(time).length());
        buf[20] = (byte)queryType;
        if(queryCode != null)
            System.arraycopy(queryCode.getBytes(), 0, buf, 21, queryCode.length());
        strBuf = new StringBuffer(200);
        strBuf.append(",time=".concat(String.valueOf(String.valueOf(time))));
        strBuf.append(",Query_Type=".concat(String.valueOf(String.valueOf(queryType))));
        strBuf.append(",Query_Code=".concat(String.valueOf(String.valueOf(queryCode))));
    }

    public String toString()
    {
        StringBuffer outStr = new StringBuffer(200);
        outStr.append("SMGPQueryMessage:");
        outStr.append("PacketLength=".concat(String.valueOf(String.valueOf(buf.length))));
        outStr.append(",RequestID=7");
        outStr.append(",Sequence_Id=".concat(String.valueOf(String.valueOf(getSequenceId()))));
        if(strBuf != null)
            outStr.append(strBuf.toString());
        return outStr.toString();
    }

    public int getRequestId()
    {
        return SMGPCommon.SMGP_QUERY;
    }
}
